package com.youkeyi.ddy.cloud.application.system.corp.controller;

import com.youkeyi.ddy.cloud.common.api.application.system.corp.SystemCorpCloudService;
import com.youkeyi.ddy.cloud.common.api.application.system.corp.dto.SystemCorpListCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.system.corp.dto.SystemCorpListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.system.corp.dto.SystemCorpUserListCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.system.corp.dto.SystemCorpUserListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.system.corp.param.*;
import com.youkeyi.ddy.cloud.common.api.basic.crm.config.CrmCustomerConfigCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.crm.config.param.CrmCustomerConfigCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.pub.corp.PublicCorpCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.pub.corp.dto.PublicCorpListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.pub.corp.dto.PublicCorpListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.pub.corp.param.PublicCorpCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.pub.corp.param.PublicCorpDeleteCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.pub.corp.param.PublicCorpQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.pub.corp.param.PublicCorpUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.pub.mapping.PublicMappingCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.pub.mapping.dto.PublicMappingListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.pub.mapping.dto.PublicMappingListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.pub.mapping.param.PublicMappingBusinessUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.pub.mapping.param.PublicMappingQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.pub.organization.PublicOrganizationCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.pub.organization.dto.PublicOrganizationListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.pub.organization.dto.PublicOrganizationListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.pub.organization.param.PublicOrganizationQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.pub.position.dto.PublicPositionListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.pub.user.PublicUserCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.pub.user.dto.PublicUserListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.pub.user.param.PublicUserCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.pub.user.param.PublicUserQueryCloudParam;
import com.youkeyi.ddy.cloud.common.config.TestCorpConfig;
import com.youkeyi.ddy.cloud.common.constant.YoukeyiDdyCloudBasicPublicConstant;
import com.youkeyi.ddy.cloud.common.context.YoukeyiSystemContext;
import com.youkeyi.ddy.cloud.common.enums.ResponseCodeEnum;
import com.youkeyi.ddy.cloud.common.response.ResponseContent;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * SystemCorpController
 *
 * @Corpor liuxiawang
 * @Date 2024/2/27 5:20 PM
 * @Location shenzhen.china
 */
@Service
public class SystemCorpController implements SystemCorpCloudService {
    
    @Autowired
    private PublicCorpCloudService publicCorpCloudService;
    @Autowired
    private PublicUserCloudService publicUserCloudService;
    @Autowired
    private CrmCustomerConfigCloudService crmCustomerConfigCloudService;
    @Autowired
    private PublicOrganizationCloudService publicOrganizationCloudService;
    @Autowired
    private PublicMappingCloudService publicMappingCloudService;

    @Override
    public SystemCorpListCloudDto list(SystemCorpListCloudParam param) {
        SystemCorpListCloudDto dto = new SystemCorpListCloudDto();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            dto.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return dto;
        }
        if (null == param) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        PublicCorpQueryCloudParam corpQueryCloudParam = new PublicCorpQueryCloudParam();
        // 非测试主体就查询的时候剔除测试主体
        if (!TestCorpConfig.TestCorpKey.contains(corpKey)) {
            corpQueryCloudParam.setCorpPrimaryKeys(Collections.singletonList(corpKey));
        }
        corpQueryCloudParam.setNameFuzzy(param.getNameFuzzy());
        corpQueryCloudParam.setNames(param.getNames());
        corpQueryCloudParam.setCodeFuzzy(param.getCodeFuzzy());
        corpQueryCloudParam.setCodes(param.getCodes());
        corpQueryCloudParam.setUserNameFuzzy(param.getUserNameFuzzy());
        corpQueryCloudParam.setUserPhoneFuzzy(param.getUserPhoneFuzzy());
        corpQueryCloudParam.setTypes(param.getTypes());
        corpQueryCloudParam.setPageSize(param.getPageSize());
        corpQueryCloudParam.setCurrentPage(param.getCurrentPage());
        PublicCorpListCloudDto corpDtos = publicCorpCloudService.query(corpQueryCloudParam);
        return getCorp(corpDtos);
    }

    @Override
    public SystemCorpListCloudDto financeList() {
        SystemCorpListCloudDto dto = new SystemCorpListCloudDto();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            dto.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return dto;
        }

        PublicCorpQueryCloudParam corpQueryCloudParam = new PublicCorpQueryCloudParam();
        corpQueryCloudParam.setTypes(Collections.singletonList(YoukeyiDdyCloudBasicPublicConstant.PUBLIC_CORP_TYPE_3.value()));
        PublicCorpListCloudDto corpDtos = publicCorpCloudService.query(corpQueryCloudParam);

        PublicCorpQueryCloudParam corpOtherQueryCloudParam = new PublicCorpQueryCloudParam();
        corpOtherQueryCloudParam.setTypes(Collections.singletonList(YoukeyiDdyCloudBasicPublicConstant.PUBLIC_CORP_TYPE_4.value()));
        corpOtherQueryCloudParam.setCategories(Collections.singletonList(YoukeyiDdyCloudBasicPublicConstant.PUBLIC_CORP_CATEGORY_10.value()));
        PublicCorpListCloudDto corpOtherDtos = publicCorpCloudService.query(corpOtherQueryCloudParam);

        if (null != corpDtos && CollectionUtils.isNotEmpty(corpDtos.getRows())) {
            corpDtos.getRows().addAll(corpOtherDtos.getRows());
            return getCorp(corpDtos);
        } else {
            return getCorp(corpOtherDtos);
        }
    }

    private SystemCorpListCloudDto getCorp(PublicCorpListCloudDto corpDtos) {
        SystemCorpListCloudDto dto = new SystemCorpListCloudDto();
        if (null != corpDtos && CollectionUtils.isNotEmpty(corpDtos.getRows())) {
            List<SystemCorpListItemCloudDto> items = new ArrayList<>();
            corpDtos.getRows().forEach(corp -> {
                SystemCorpListItemCloudDto item = new SystemCorpListItemCloudDto();
                item.setCorpPrimaryKey(corp.getCorpPrimaryKey());
                item.setName(corp.getName());
                item.setCode(corp.getCode());
                item.setCategory(corp.getCategory());
                item.setCategoryName(YoukeyiDdyCloudBasicPublicConstant.PUBLIC_CORP_CATEGORY.getName(item.getCategory()));
                item.setType(corp.getType());
                item.setTypeName(YoukeyiDdyCloudBasicPublicConstant.PUBLIC_CORP_TYPE.getName(item.getType()));
                item.setAuthType(corp.getAuthType());
                item.setAuthTypeName(YoukeyiDdyCloudBasicPublicConstant.PUBLIC_CORP_AUTH_TYPE.getName(item.getAuthType()));
                item.setEffectiveTime(corp.getEffectiveTime());
                item.setExpiredTime(corp.getExpiredTime());
                item.setAppKey(corp.getAppKey());
                item.setAppSecret(corp.getAppSecret());
                item.setUserName(corp.getUserName());
                item.setUserPhone(corp.getUserPhone());
                item.setEmail(corp.getEmail());
                item.setBusinessLinceseName(corp.getBusinessLinceseName());
                item.setBusinessLicenseNo(corp.getBusinessLicenseNo());
                item.setLeftLogo(corp.getLeftLogo());
                item.setRightLogo(corp.getRightLogo());
                item.setManagementAttribution(corp.getManagementAttribution());
                item.setCoordinate(corp.getCoordinate());
                item.setIsDeleted(corp.getIsDeleted());
                item.setIsDeletedName(corp.getIsDeletedName());
                item.setCreateUserKey(corp.getCreateUserKey());
                item.setCreateTime(corp.getCreateTime());
                item.setUpdateUserKey(corp.getUpdateUserKey());
                item.setUpdateTime(corp.getUpdateTime());
                item.setCreateUserName(corp.getCreateUserName());
                item.setUpdateUserName(corp.getUpdateUserName());
                items.add(item);
            });
            dto.setRows(items);
            dto.setTotal(corpDtos.getTotal());
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    @Override
    public ResponseContent create(SystemCorpCreateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param
                || StringUtils.isBlank(param.getName())
        ) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        PublicCorpCreateCloudParam corpCreateCloudParam = new PublicCorpCreateCloudParam();
        corpCreateCloudParam.setName(param.getName());
        corpCreateCloudParam.setCode(param.getCode());
        corpCreateCloudParam.setCategory(param.getCategory());
        corpCreateCloudParam.setType(param.getType());
        corpCreateCloudParam.setAuthType(param.getAuthType());
        corpCreateCloudParam.setEffectiveTime(param.getEffectiveTime());
        corpCreateCloudParam.setExpiredTime(param.getExpiredTime());
        corpCreateCloudParam.setAppKey(param.getAppKey());
        corpCreateCloudParam.setAppSecret(param.getAppSecret());
        corpCreateCloudParam.setUserName(param.getUserName());
        corpCreateCloudParam.setUserPhone(param.getUserPhone());
        corpCreateCloudParam.setEmail(param.getEmail());
        corpCreateCloudParam.setBusinessLinceseName(param.getBusinessLinceseName());
        corpCreateCloudParam.setBusinessLicenseNo(param.getBusinessLicenseNo());
        corpCreateCloudParam.setLeftLogo(param.getLeftLogo());
        corpCreateCloudParam.setRightLogo(param.getRightLogo());
        corpCreateCloudParam.setManagementAttribution(param.getManagementAttribution());
        corpCreateCloudParam.setCoordinate(param.getCoordinate());
        corpCreateCloudParam.setCreateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        corpCreateCloudParam.setCreateUserName(YoukeyiSystemContext.getUserName());
        ResponseContent corpCreateDto = publicCorpCloudService.create(corpCreateCloudParam);
        if (null != corpCreateDto && StringUtils.isNotBlank(corpCreateDto.getKey())) {
            // 创建客户释放规则
            CrmCustomerConfigCreateCloudParam configCreateCloudParam = new CrmCustomerConfigCreateCloudParam();
            configCreateCloudParam.setCorpKey(corpCreateDto.getKey());
            configCreateCloudParam.setCreateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
            configCreateCloudParam.setCreateUserName(YoukeyiSystemContext.getUserName());
            crmCustomerConfigCloudService.create(configCreateCloudParam);

            if (StringUtils.isNotBlank(param.getUserName()) && StringUtils.isNotBlank(param.getUserPhone())) {
                PublicUserCreateCloudParam userCreateCloudParam = new PublicUserCreateCloudParam();
                userCreateCloudParam.setCorpKey(corpCreateDto.getKey());
                userCreateCloudParam.setName(param.getUserName());
                userCreateCloudParam.setNickName(param.getUserName());
                userCreateCloudParam.setPhone(param.getUserPhone());
                userCreateCloudParam.setIsRoot(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_YES.value());
                userCreateCloudParam.setEmail(param.getEmail());
                userCreateCloudParam.setCreateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
                userCreateCloudParam.setCreateUserName(YoukeyiSystemContext.getUserName());
                return publicUserCloudService.create(userCreateCloudParam);
            }
        }
        return corpCreateDto;
    }

    @Override
    public ResponseContent update(SystemCorpUpdateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param
                || StringUtils.isBlank(param.getCorpPrimaryKey())
                || StringUtils.isBlank(param.getName())
        ) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        PublicCorpUpdateCloudParam corpUpdateCloudParam = new PublicCorpUpdateCloudParam();
        corpUpdateCloudParam.setCorpPrimaryKey(param.getCorpPrimaryKey());
        corpUpdateCloudParam.setName(param.getName());
        corpUpdateCloudParam.setCode(param.getCode());
        corpUpdateCloudParam.setCategory(param.getCategory());
        corpUpdateCloudParam.setType(param.getType());
        corpUpdateCloudParam.setAuthType(param.getAuthType());
        corpUpdateCloudParam.setEffectiveTime(param.getEffectiveTime());
        corpUpdateCloudParam.setExpiredTime(param.getExpiredTime());
        corpUpdateCloudParam.setAppKey(param.getAppKey());
        corpUpdateCloudParam.setAppSecret(param.getAppSecret());
        corpUpdateCloudParam.setUserName(param.getUserName());
        corpUpdateCloudParam.setUserPhone(param.getUserPhone());
        corpUpdateCloudParam.setEmail(param.getEmail());
        corpUpdateCloudParam.setBusinessLinceseName(param.getBusinessLinceseName());
        corpUpdateCloudParam.setBusinessLicenseNo(param.getBusinessLicenseNo());
        corpUpdateCloudParam.setLeftLogo(param.getLeftLogo());
        corpUpdateCloudParam.setRightLogo(param.getRightLogo());
        corpUpdateCloudParam.setManagementAttribution(param.getManagementAttribution());
        corpUpdateCloudParam.setCoordinate(param.getCoordinate());
        corpUpdateCloudParam.setUpdateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        corpUpdateCloudParam.setUpdateUserName(YoukeyiSystemContext.getUserName());
        return publicCorpCloudService.update(corpUpdateCloudParam);
    }

    @Override
    public ResponseContent delete(SystemCorpDeleteCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param || CollectionUtils.isEmpty(param.getCorpPrimaryKeys())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        PublicCorpDeleteCloudParam corpDeleteCloudParam = new PublicCorpDeleteCloudParam();
        corpDeleteCloudParam.setCorpPrimaryKeys(param.getCorpPrimaryKeys());
        corpDeleteCloudParam.setUpdateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        corpDeleteCloudParam.setUpdateUserName(YoukeyiSystemContext.getUserName());
        return publicCorpCloudService.delete(corpDeleteCloudParam);
    }


    @Override
    public SystemCorpListCloudDto hrMappingList(SystemHrCorpMappingListCloudParam param) {
        SystemCorpListCloudDto dto = new SystemCorpListCloudDto();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            dto.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return dto;
        }
        if (null == param || StringUtils.isBlank(param.getHrCorpPrimaryKey())) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        // 主体-菜单映射关系
        PublicMappingQueryCloudParam publicMappingQueryCloudParam = new PublicMappingQueryCloudParam();
        publicMappingQueryCloudParam.setMappingType(YoukeyiDdyCloudBasicPublicConstant.PUBLIC_MAPPING_TYPE_HRCORP_CORP.value());
        publicMappingQueryCloudParam.setKeyType(YoukeyiDdyCloudBasicPublicConstant.PUBLIC_MAPPING_KEY_TYPE_SOURCE.value());
        publicMappingQueryCloudParam.setKeys(Collections.singletonList(param.getHrCorpPrimaryKey()));
        PublicMappingListCloudDto publicMappingDtos = publicMappingCloudService.query(publicMappingQueryCloudParam);
        if (null != publicMappingDtos && CollectionUtils.isNotEmpty(publicMappingDtos.getRows())) {
            List<String> corpKeys = publicMappingDtos.getRows().stream().map(PublicMappingListItemCloudDto::getTargetKey).collect(Collectors.toList());

            PublicCorpQueryCloudParam corpQueryCloudParam = new PublicCorpQueryCloudParam();
            corpQueryCloudParam.setCorpPrimaryKeys(corpKeys);
            PublicCorpListCloudDto corpDtos = publicCorpCloudService.query(corpQueryCloudParam);
            return getCorp(corpDtos);
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    @Override
    public ResponseContent hrMappingUpdate(SystemHrCorpMappingUpdateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param || StringUtils.isBlank(param.getHrCorpPrimaryKey()) || CollectionUtils.isEmpty(param.getCorpPrimaryKeys())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        // 主体-菜单映射关系
        PublicMappingBusinessUpdateCloudParam mappingBusinessUpdateCloudParam = new PublicMappingBusinessUpdateCloudParam();
        mappingBusinessUpdateCloudParam.setMappingType(YoukeyiDdyCloudBasicPublicConstant.PUBLIC_MAPPING_TYPE_HRCORP_CORP.value());
        mappingBusinessUpdateCloudParam.setKeyType(YoukeyiDdyCloudBasicPublicConstant.PUBLIC_MAPPING_KEY_TYPE_SOURCE.value());
        mappingBusinessUpdateCloudParam.setKeyTypeKey(param.getHrCorpPrimaryKey());
        mappingBusinessUpdateCloudParam.setChangeKeys(param.getCorpPrimaryKeys());
        mappingBusinessUpdateCloudParam.setCreateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        mappingBusinessUpdateCloudParam.setCreateUserName(YoukeyiSystemContext.getUserName());
        return publicMappingCloudService.businessUpdate(mappingBusinessUpdateCloudParam);
    }


    @Override
    public SystemCorpUserListCloudDto userList(SystemCorpListCloudParam param) {
        SystemCorpUserListCloudDto dto = new SystemCorpUserListCloudDto();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            dto.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return dto;
        }
        if (null == param) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        PublicCorpQueryCloudParam corpQueryCloudParam = new PublicCorpQueryCloudParam();
        // 非测试主体就查询的时候剔除测试主体
        if (!TestCorpConfig.TestCorpKey.contains(corpKey)) {
            corpQueryCloudParam.setNotInCorpPrimaryKeys(Arrays.asList(TestCorpConfig.TestCorpKey.split("\\|")));
        }
        corpQueryCloudParam.setNameFuzzy(param.getNameFuzzy());
        corpQueryCloudParam.setNames(param.getNames());
        corpQueryCloudParam.setCodeFuzzy(param.getCodeFuzzy());
        corpQueryCloudParam.setCodes(param.getCodes());
        corpQueryCloudParam.setUserNameFuzzy(param.getUserNameFuzzy());
        corpQueryCloudParam.setUserPhoneFuzzy(param.getUserPhoneFuzzy());
        corpQueryCloudParam.setTypes(param.getTypes());
        PublicCorpListCloudDto corpDtos = publicCorpCloudService.query(corpQueryCloudParam);
        if (null != corpDtos && CollectionUtils.isNotEmpty(corpDtos.getRows())) {
            Map<String, PublicCorpListItemCloudDto> corpMap = corpDtos.getRows().stream().collect(Collectors.toMap(PublicCorpListItemCloudDto::getCorpPrimaryKey, Function.identity(), (k1, k2) -> k1));

            PublicOrganizationQueryCloudParam organizationQueryCloudParam = new PublicOrganizationQueryCloudParam();
            organizationQueryCloudParam.setNeedShare(Boolean.TRUE);
            PublicOrganizationListCloudDto organizationDtos = publicOrganizationCloudService.query(organizationQueryCloudParam);
            Map<String, PublicOrganizationListItemCloudDto> organizationMap = new HashMap<>();
            if (null != organizationDtos && CollectionUtils.isNotEmpty(organizationDtos.getRows())) {
                organizationMap.putAll(organizationDtos.getRows().stream().collect(Collectors.toMap(PublicOrganizationListItemCloudDto::getOrganizationPrimaryKey, Function.identity(), (k1, k2) -> k1)));
            }

            PublicUserQueryCloudParam userQueryCloudParam = new PublicUserQueryCloudParam();
            userQueryCloudParam.setCorpKeys(corpDtos.getRows().stream().map(PublicCorpListItemCloudDto::getCorpPrimaryKey).collect(Collectors.toList()));
            userQueryCloudParam.setNeedPosition(Boolean.TRUE);
            PublicUserListCloudDto userDtos = publicUserCloudService.query(userQueryCloudParam);
            if (null != userDtos && CollectionUtils.isNotEmpty(userDtos.getRows())) {
                List<SystemCorpUserListItemCloudDto> items = new ArrayList<>();
                List<SystemCorpUserListItemCloudDto> selfCorpItems = new ArrayList<>();
                userDtos.getRows().forEach(user -> {
                    if (corpMap.containsKey(user.getCorpKey())) {
                        PublicCorpListItemCloudDto corp = corpMap.get(user.getCorpKey());
                        SystemCorpUserListItemCloudDto item = new SystemCorpUserListItemCloudDto();
                        item.setCorpPrimaryKey(corp.getCorpPrimaryKey());
                        item.setCorpName(corp.getName());
                        item.setCorpCode(corp.getCode());
                        item.setCorpType(corp.getType());
                        item.setCorpTypeName(YoukeyiDdyCloudBasicPublicConstant.PUBLIC_CORP_TYPE.getName(item.getCorpType()));
                        item.setUserPrimaryKey(user.getUserPrimaryKey());
                        item.setUserCode(user.getCode());
                        item.setUserName(user.getName());
                        item.setUserNickName(user.getNickName());
                        item.setUserPhone(user.getPhone());
                        if (CollectionUtils.isNotEmpty(user.getPositions())) {
                            item.setPositionPrimaryKey(user.getPositions().get(0).getPositionPrimaryKey());
                            item.setPositionName(user.getPositions().get(0).getName());
                        }
                        if (CollectionUtils.isNotEmpty(user.getPositions())) {
                            PublicPositionListItemCloudDto position = user.getPositions().get(0);
                            if (organizationMap.containsKey(position.getOrganizationKey())) {
                                PublicOrganizationListItemCloudDto organization = organizationMap.get(position.getOrganizationKey());
                                item.setOrganizationPrimaryKey(position.getOrganizationKey());
                                item.setOrganizationName(organization.getName());
                                item.setOrganizationSort(organization.getSort());
                            }
                        }
                        if (!corp.getCorpPrimaryKey().equals(corpKey)) {
                            items.add(item);
                        } else {
                            selfCorpItems.add(item);
                        }
                    }
                });
                if (CollectionUtils.isNotEmpty(items)) {
                    items.sort(Comparator.comparing(SystemCorpUserListItemCloudDto::getOrganizationSort, Comparator.nullsLast(Integer::compareTo)));
                }
                dto.setRows(items);
                if (CollectionUtils.isNotEmpty(selfCorpItems)) {
                    items.sort(Comparator.comparing(SystemCorpUserListItemCloudDto::getOrganizationSort, Comparator.nullsLast(Integer::compareTo)).reversed());
                    selfCorpItems.forEach(item -> dto.getRows().add(0, item));
                }
            }
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    @Override
    public SystemCorpUserListCloudDto mappingCorpUserList(SystemCorpListCloudParam param) {
        SystemCorpUserListCloudDto dto = new SystemCorpUserListCloudDto();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            dto.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return dto;
        }
        if (null == param || CollectionUtils.isNotEmpty(param.getCorpPrimaryKeys())) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        // 主体-菜单映射关系
        PublicMappingQueryCloudParam publicMappingQueryCloudParam = new PublicMappingQueryCloudParam();
        publicMappingQueryCloudParam.setMappingType(YoukeyiDdyCloudBasicPublicConstant.PUBLIC_MAPPING_TYPE_HRCORP_CORP.value());
        publicMappingQueryCloudParam.setKeyType(YoukeyiDdyCloudBasicPublicConstant.PUBLIC_MAPPING_KEY_TYPE_SOURCE.value());
        publicMappingQueryCloudParam.setKeys(Collections.singletonList(param.getCorpPrimaryKeys().get(0)));
        PublicMappingListCloudDto publicMappingDtos = publicMappingCloudService.query(publicMappingQueryCloudParam);
        if (null == publicMappingDtos || CollectionUtils.isEmpty(publicMappingDtos.getRows())) {
            dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
            return dto;
        }
        List<String> corpKeys = publicMappingDtos.getRows().stream().map(PublicMappingListItemCloudDto::getTargetKey).collect(Collectors.toList());

        SystemCorpListCloudParam systemCorpListCloudParam = new SystemCorpListCloudParam();
        systemCorpListCloudParam.setCorpPrimaryKeys(corpKeys);
        return this.userList(systemCorpListCloudParam);
    }
}
